package com.lagou.mapper;

import com.lagou.pojo.User;
import org.apache.ibatis.annotations.*;
import org.mybatis.caches.redis.RedisCache;

import java.util.List;

@CacheNamespace(implementation = RedisCache.class)
public interface UserAnnotationMapper {
    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "orderList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lagou.mapper.OrderAnnotationMapper.findByUid"))
    })
    List<User> findAll();


    @Select("select * from user")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "username", column = "username"),
            @Result(property = "password", column = "password"),
            @Result(property = "birthday", column = "birthday"),
            @Result(property = "roleList", column = "id", javaType = List.class,
                    many = @Many(select = "com.lagou.mapper.RoleAnnotationMapper.selectRoleByUserid"))
    })
    List<User> findWithRole();


    // https://blog.csdn.net/weixin_39214481/article/details/80366725
    @Insert("insert into user values(#{id},#{username},#{password},#{birthday})")
    int insertUser(User user);

    @Delete("delete from user where id = #{id}")
    int deleteUser(Integer id);

    @Update("update user set username=#{username},password=#{password},birthday=#{birthday} where id=#{id}")
    int updateUser(User user);

    @Select("select * from user")
    List<User> selectUser();

    @Select("select * from user where id=#{id}")
    User selectUserById(Integer id);
}